import {getSysConfig} from "@/api/common"

import config from "@/config.js"
import $wxUtils from "@/utils/wxUtil.js"

const {platform} = wx.getSystemInfoSync()

const state = {
  platform,
  globalTitle: "服务运维管理系统", // 全局标题
  pageTitle: config.title,
  statusBarHeight: 0, // 状态栏宽度
  navHeight: 0, // 导航栏高度
  navTop: 0, // 胶囊与顶部的距离
  windowWidth: 0, // 设备宽度
  windowHeight: 0, // 设备高度
  contentHeight: 0, // 设备可显示区域
  iosFun: false, // ios
  tabbar: "index", //路由组标记
  tabbarKey: "/pages/index/index", // 当前路由
  tabbarList: [],
  tabbars: {
    index: [
      {
        title: "产品",
        icon: "shopping-cart",
        name: "index",
        path: "/pages/index/index"
      },
      {
        title: "我的",
        icon: "account-fill",
        name: "my",
        path: "/pages/my/my"
      }
    ]
  },
  accountType: "",
  toast: false,
  toastText: ""
}

const mutations = {
  SET_CONFIGS(state, configs) {
    for (const k in state) {
      if (typeof configs[k] != "undefined") {
        state[k] = configs[k]
        if (k == "isShow") {
          state.sustain = !state.sustain
        }
      }
    }
  },
  SET_MESSAGE(state, args) {
    state.message = args
  },
  SET_TABBAR(state, args) {
    state.tabbarKey = args
  },
  SET_ROUTER(state, args) {
    let nowKey = ""
    let oldKey = state.tabbarKey
    // let codes = args.codes
    // let flag = args.flag
    nowKey = args.routePath

    console.log("路由组切换", args, nowKey, oldKey)

    if (oldKey != nowKey) {
      state.route = args.route
      state.tabbarKey = nowKey
      state.tabbarList = state.tabbars[args.route]

      $wxUtils.go(args.route)
    } else {
      state.tabbarList = state.tabbars[args.route]
    }
    uni.hideLoading()
  }
}

const actions = {
  // 初始化程序
  init({commit}) {
    uni.getSystemInfo({
      success: res => {
        let menuButtonObject = {
          top: 0,
          height: 44
        }
        // #ifdef MP
        menuButtonObject = uni.getMenuButtonBoundingClientRect() // 右侧胶囊信息
        // #endif
        // console.log('menuButtonObject', menuButtonObject, res)
        // uni.getSystemInfo({
        //   success (res) {
        let statusBarHeight = res.statusBarHeight // 状态栏高度
        let navTop = menuButtonObject.top // 胶囊与顶部的距离
        let navHeight = statusBarHeight + menuButtonObject.height + (navTop - statusBarHeight) * 2 // 导航栏高度
        let windowHeight = res.windowHeight
        let windowWidth = res.windowWidth
        let contentHeight = windowHeight - navHeight
        let iosFun = res.system.indexOf("iOS 10") > -1

        // #ifdef H5
        navTop = 7
        // #endif

        commit("SET_CONFIGS", {
          statusBarHeight,
          navTop,
          navHeight,
          windowHeight,
          windowWidth,
          contentHeight,
          iosFun
        })

        console.log("update config")

        //   }
        // })
      }
    })
  },
  // 设置 configs
  syncSetConfigs({commit}, args) {
    commit("SET_CONFIGS", args)
  },
  // tabbar
  setTabbar({commit}, args) {
    commit("SET_TABBAR", args)
  },
  // router
  setRouter({commit}, args) {
    commit("SET_ROUTER", args)
  },
  // 获取SOM系统信息
  getSysConfig({commit}) {
    getSysConfig()
      .then(res => {
        // commit('SET_MESSAGE', res.data.message || '')
        // commit('SET_HOTLINE', res.data.configAttr.hotline || '')
      })
      .catch(_ => {})
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
